/**
 * @Title: ExcelController.java
 * @Package com.lemon.controller.excel
 * @Description: (一句话阐述):
 * @author Ran_cc
 * @date 2018年8月10日
 * @version V1.0
 */
package com.lemon.controller.excel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * Title: ExcelController
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author Ran_cc
 * @date 2018年8月10日
 * @版本 V 1.0
 */
@RestController
@RequestMapping("/excel")
public class ExcelController {

	@Autowired(required = true)
	private ExcelService excelService;

	//导入excel
	@RequestMapping(value = "/import" , method = RequestMethod.POST)
	public Map<String , Object> importExcel(@RequestParam(value = "file" , required = false) MultipartFile file ,
			HttpServletRequest request , HttpServletResponse response) {

		Map<String , Object> map = new HashMap<String , Object>();
		String result = excelService.readExcelFile(file);
		map.put("message" , result);
		return map;
	}

	//导入excel
	@RequestMapping(value = "/pintuan" , method = RequestMethod.POST)
	public Map<String , Object> pintuan(@RequestParam(value = "file" , required = false) MultipartFile file ,
			HttpServletRequest request , HttpServletResponse response) {

		Map<String , Object> map = new HashMap<String , Object>();
		String result = excelService.readPinTuanExcelFile(file);
		map.put("message" , result);
		return map;
	}

	@RequestMapping(value = "/cityDownExcel.do")
	public void cityDownExcel(HttpServletResponse response,HttpServletRequest request) {
        try {
            //获取文件的路径
			//String excelPath = request.getSession().getServletContext().getRealPath("/Excel/"+"荒.xls");
			String fileName = "城跑-菜品批量导入模板.xlsx".toString(); // 文件的默认保存名
            // 读到流中
			InputStream inStream = new FileInputStream("src/main/resources/files/城跑-菜品批量导入模板.xlsx");//文件的存放路径
            // 设置输出的格式
            response.reset();
            response.setContentType("bin");
            response.addHeader("Content-Disposition",
					"attachment;filename=" + URLEncoder.encode(fileName , "UTF-8"));
            // 循环取出流中的数据
            byte[] b = new byte[200];
            int len;
 
            while ((len = inStream.read(b)) > 0){
                response.getOutputStream().write(b, 0, len);
            }
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

}
